package glemipsc.implementations.glemips32;

import glemipsc.GLEMipsEmu;
import glemipsc.implementations.Argument;
import glemipsc.implementations.Instruction;

public class Mov extends glemipsc.implementations.Instruction {

	public Mov(Argument dest, Argument source) {
		this.addArgument(dest);
		this.addArgument(source);
	}

	@Override
	public String getOpCode() {
		String opcode;
		
		if (arguments.get(1) instanceof Register) {
			// Register to Register
			opcode = Instruction.ALU;
			opcode += "00000";
			opcode += arguments.get(1).getOpCode();
			opcode += arguments.get(0).getOpCode();
			opcode += "00000000000";
		}
		else {
			// Immediate
			opcode = Instruction.MOV;
			opcode += "00000";
			opcode += arguments.get(0).getOpCode();
			opcode += arguments.get(1).getOpCode();
		}
		return opcode + "\n";
	}

	@Override
	public String getName() {
		return "MOV";
	}

	@Override
	public void emulate(GLEMipsEmu emu) {
		Register dest = (Register) arguments.get(0);
		if (arguments.get(1) instanceof Register)
			dest.setEmulValue(((Register) arguments.get(1)).getEmulValue());
		else
			dest.setEmulValue(((Immediate) arguments.get(1)).n);
	}

}
